package cn.tedu.store.config.QRCode;

import cn.hutool.core.io.FastByteArrayOutputStream;
import cn.hutool.extra.qrcode.QrCodeUtil;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Base64;

public class QRCodeUtil {
    public static String getCode(String codeUrl) {
        // 二维码内容
        String url = codeUrl;
        // 生成二维码并指定宽高
        BufferedImage generate = QrCodeUtil.generate(url, 300, 300);
        // 转换流信息写出
        FastByteArrayOutputStream os = new FastByteArrayOutputStream();
        try {
            ImageIO.write(generate, "jpg", os);
        } catch (IOException e) {
            return e.getMessage();
        }
        //如果二维码要在前端显示需要转成Base64
        return Base64.getEncoder().encodeToString(os.toByteArray());
    }

    public static void main(String[] args) {
        //
        String url="|硬件|";
        //String name="百度";
        String code = QRCodeUtil.getCode(url);
        System.out.println(code);
    }
}
